package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.LevelListViewAdapter;
import com.digitalchina.lxnsy.api.BusinessPersonnelService;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.db.DBhelper;
import com.digitalchina.lxnsy.db.Level;
import com.digitalchina.lxnsy.entity.TalentTitleVO;
import com.digitalchina.lxnsy.fragment.PersonnelDemandFragment;
import com.digitalchina.lxnsy.fragment.SheepFragment;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.kekstudio.dachshundtablayout.DachshundTabLayout;
import com.kekstudio.dachshundtablayout.indicators.LineMoveIndicator;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.digitalchina.lxnsy.R.id.lv_city;
import static com.digitalchina.lxnsy.R.id.lv_province;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/9/8 10:26
 * 修改备注：
 * ================================================
 */
public class SheepListActivity extends BaseActivity {

    @Bind(R.id.tab_layout)
    DachshundTabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @Bind(R.id.flow_layout)
    TagFlowLayout flowLayout;
    @Bind(lv_province)
    ListView lvProvince;
    @Bind(lv_city)
    ListView lvCity;
    @Bind(R.id.lv_county)
    ListView lvCounty;
    @Bind(R.id.edit_low_price)
    EditText editLowPrice;
    @Bind(R.id.edit_high_price)
    EditText editHighPrice;
    @Bind(R.id.view1)
    View view1;
    @Bind(R.id.view2)
    View view2;
    @Bind(R.id.view3)
    View view3;

    private MyPagerAdapter adapter;
    private List<Fragment> fragments = new ArrayList<>();
    private String title[] = {"良种羊供应","良种羊需求"};
    private boolean myTalents = false;
    private List<TalentTitleVO> titleVOList = new ArrayList<>();
    private TagAdapter<TalentTitleVO> tagAdapter;


    private int viewPagerIndex = 0;
    private String titleCode = "";
    private String cityCode = "";
    private String countyCode = "";
    private String lowPrice = "";
    private String highPrice = "";


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_personnel_service_list);
    }

    @Override
    public void initMember() {


        tabLayout.setAnimatedIndicator(new LineMoveIndicator(tabLayout));

        if (getIntent().hasExtra("key") && getIntent().getSerializableExtra("key").equals("mytalents")) {
            //从我的页面跳转
            myTalents = true;
        } else {
            myTalents = false;
        }
        setTitle("良种羊交易");
        //setRightText("筛选");

        //获取招聘职称
        BusinessPersonnelService.getTalentTitles(this, mHandler);

        for (int i = 0; i < title.length; i++) {
            SheepFragment fragment = new SheepFragment();
            fragments.add(fragment);
            Bundle bundle = new Bundle();
            bundle.putString("postType", String.valueOf(i));
            bundle.putBoolean("myTalents", myTalents);
            fragment.setArguments(bundle);
        }

        adapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setOffscreenPageLimit(title.length);
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
    }

    /**
     * 初始化或者重置时隐藏List
     */
    private void hideView() {
        lvCity.setVisibility(View.INVISIBLE);
        lvCounty.setVisibility(View.INVISIBLE);
        view2.setVisibility(View.INVISIBLE);
    }
    /**
     * 显示List
     */
    private void showView() {
        lvCity.setVisibility(View.VISIBLE);
        lvCounty.setVisibility(View.VISIBLE);
        view2.setVisibility(View.VISIBLE);
    }


    @Override
    public void addListener() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewPagerIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                titleCode = titleVOList.get(position).getCode();
                return false;
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_TALENT_TITLES_SUCCESS:// 人才职称
                        callBack(msg.obj);
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        titleVOList = FastJsonUtil.getListBean(object.toString(), TalentTitleVO.class);

        tagAdapter = new TagAdapter<TalentTitleVO>(titleVOList) {

            @Override
            public View getView(FlowLayout parent, int position, TalentTitleVO vo) {
                TextView tv = (TextView) mInflater.inflate(R.layout.view_text_title,
                        flowLayout, false);
                tv.setText(vo.getName());
                return tv;
            }
        };

        flowLayout.setAdapter(tagAdapter);
    }

    @Override
    public void rightOnClick() {

        if (drawerLayout.isDrawerVisible(GravityCompat.END)) {
            drawerLayout.closeDrawer(GravityCompat.END);
        } else
            drawerLayout.openDrawer(GravityCompat.END);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_reset, R.id.tv_confirm})
    public void onViewClicked(View view) {
        PersonnelDemandFragment f = (PersonnelDemandFragment) fragments.get(viewPagerIndex);
        switch (view.getId()) {
            case R.id.tv_reset:
                // 重置
                titleCode = "";
                cityCode = "";
                countyCode = "";
                lowPrice = "";
                highPrice = "";
                tagAdapter.setSelectedList();
                editLowPrice.setText("");
                editHighPrice.setText("");
                hideView();
                break;
            case R.id.tv_confirm:
                // 确定搜索
                lowPrice = editLowPrice.getText().toString();
                highPrice = editHighPrice.getText().toString();
                f.setValue(titleCode, cityCode,countyCode, lowPrice, highPrice);
                drawerLayout.closeDrawer(GravityCompat.END);
                break;
        }
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        private MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (drawerLayout.isDrawerVisible(GravityCompat.END)) {
                drawerLayout.closeDrawer(GravityCompat.END);
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
